﻿using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace nplJobsDAL.Select
{
    public class SelectUserByID : DataAccessBase
    {
        public Guid UserId { get; set; }
        public int UserType { get; set; }

        public SelectUserByID()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectUserByID.ToString();
        }

        #region GetUser
        public int GetUser()
        {
            DataBaseHelper dbHelper;
            DataSet dsUser = new DataSet();
            int isUserExists = -1;

            try
            {
                SelectUserParameters selectUser = new SelectUserParameters(UserId, UserType);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectUser.Parameters;
                dsUser = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);

                if (dsUser.Tables[0] != null && dsUser.Tables[0].Rows.Count > 0)
                    isUserExists = dsUser != null && dsUser.Tables.Count > 0 && !string.IsNullOrEmpty(dsUser.Tables[0].Rows[0][0].ToString()) ? int.Parse(dsUser.Tables[0].Rows[0][0].ToString()) : -1;
                else
                    isUserExists = -1;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return isUserExists;
        }
        #endregion

        #region
        public class SelectUserParameters
        {
            public Guid UserId { get; set; }
            public int UserType { get; set; }

            private SqlParameter[] _parameters;

            public SelectUserParameters(Guid userId, int userType)
            {
                this.UserId = userId;
                this.UserType = userType;
                Build();
            }
            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@UserId",  this.UserId),
                    new SqlParameter("@UserType",  this.UserType),
                };

                Parameters = parameters;
            }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion
    }
}
